package cn.itcast.service.system.impl;

import cn.itcast.dao.system.ModuleDao;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class ModuleServiceImpl implements ModuleService {

    @Autowired
    private ModuleDao moduleDao;
    @Autowired
    private UserService userService;

    @Override
    public List<Module> findAll() {
        return moduleDao.findAll();
    }

    @Override
    public PageInfo<Module> findByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Module> list = moduleDao.findAll();
        return new PageInfo<>(list);
    }

    @Override
    public Module findById(String id) {
        return moduleDao.findById(id);
    }

    @Override
    public void save(Module module) {
        module.setId(UUID.randomUUID().toString());
        moduleDao.save(module);
    }

    @Override
    public void update(Module module) {
        moduleDao.update(module);
    }

    @Override
    public void delete(String id) {
        moduleDao.delete(id);
    }

    @Override
    public List<Module> findModuleByUserId(String userId) {
        //1根据用户id查询
        User user = userService.findById(userId);
        //获取degree
        Integer degree = user.getDegree();
        if (degree == 0) {
            //登陆用户的degree=0,saas管理员，只显示saas模块
            //SELECT * FROM ss_module WHERE belong=0
            return moduleDao.findByBelong(0);
        } else if(degree ==1){
            return moduleDao.findByBelong(1);
        }
        else {
            //登陆用户的degree是其他，就根据用户id查询权限
            return moduleDao.findModuleByUserId(userId);
        }

    }
}
